package com.orhon.smartcampus.modules.base.mutation;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class DeleteSchoolMutation implements DataFetcher {



    @Autowired
    SchoolsDao schoolsDao;

    /**
     * 学校信息删除
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        if(environment.getArgument("id")!=null) {
            List<Integer> ids = environment.getArgument("id");
            ids.forEach(id-> {
                        Schools item = schoolsDao.get(Schools.class, Long.valueOf(id));
                        item.setDeleted_at( new Date());
                        try
                        {
                            schoolsDao.update(item);
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
            );
            return Result.ok();
        }
        return Result.error();
    }
}

